﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
//using System.Linq;
using System.Text;
using System.Windows.Forms;
//new
using System.IO;

namespace JKZP
{
    public partial class addcamera : Form
    {
        public addcamera()
        {
            InitializeComponent();
        }

        private void addcamera_Load(object sender, EventArgs e)
        {
            this.comboBox_camtype.Items.Clear();
            foreach (string type in Enum.GetNames(typeof(PP.JKCAMERATYPE)))
            {
                this.comboBox_camtype.Items.Add(type);
            }

            this.comboBox_camposition.Items.Clear();
            foreach (string position in Enum.GetNames(typeof(PP.CAMERAPOSITION)))
            {
                this.comboBox_camposition.Items.Add(position);
            }

            this.comboBox_camroad.Items.Clear();
            if (PP.gRoadList != null)
            {
                foreach (PP.RoadParam road in PP.gRoadList)
                {
                    this.comboBox_camroad.Items.Add(road.LKID + "---" + road.LKMC);
                }
            }
        }

        private void button_save_Click(object sender, EventArgs e)
        {
            if (comboBox_camroad.Text == "")
            {
                toolStripStatusLabel1.Text = "添加失败，未选择所属路口";
                return;
            }
            if (textBox_camname.Text == "")
            {
                toolStripStatusLabel1.Text = "添加失败，未填写摄像机名称";
                return;
            }
            if (comboBox_camtype.Text == "")
            {
                toolStripStatusLabel1.Text = "添加失败，未选择摄像机类型";
                return;
            }
            if (textBox_camip.Text == "")
            {
                toolStripStatusLabel1.Text = "添加失败，未填写摄像机IP";
                return;
            }
            if (comboBox_camposition.Text == "")
            {
                toolStripStatusLabel1.Text = "添加失败，未选择摄像机位置";
                return;
            }
            if (textBox_deviceid.Text == "")
            {
                toolStripStatusLabel1.Text = "添加失败，未填写设备编号";
                return;
            }
            if (textBox_username.Text == "")
            {
                toolStripStatusLabel1.Text = "添加失败，未填写用户名";
                return;
            }
            if (textBox_password.Text == "")
            {
                toolStripStatusLabel1.Text = "添加失败，未填写密码";
                return;
            }

            DataGridView dgv = ((CameraForm)(this.Owner)).dataGridView1;
            for (int i = 0; i < dgv.Rows.Count; i++)
            {
                DataGridViewRow r = dgv.Rows[i];
                if (textBox_camip.Text == r.Cells["camip"].Value.ToString())
                {
                    toolStripStatusLabel1.Text = "添加失败，摄像机IP已存在";
                    return;
                }
            }
            string[] camroad = comboBox_camroad.Text.Split(new string[] { "---" }, StringSplitOptions.RemoveEmptyEntries);



            if (AddCam(textBox_camname.Text, textBox_camip.Text, camroad[0], camroad[1], comboBox_camtype.Text, comboBox_camposition.Text, textBox_cambz.Text, textBox_deviceid.Text, textBox_username.Text, textBox_password.Text) == true)
            {
                dgv.Rows.Add(new object[] { camroad[0], camroad[1], textBox_camname.Text, textBox_camip.Text, comboBox_camtype.Text, comboBox_camposition.Text, textBox_cambz.Text, textBox_deviceid.Text, textBox_username.Text, textBox_password.Text });
                ((Form_Main)this.Owner.Owner).UpDateTree();
                if (PP.gISWebVersion == false)
                {
                    toolStripStatusLabel1.Text = "添加成功";
                }
                else
                {
                    if (PP.kdws.JKZP_UploadRoad(File.ReadAllText(PP.gRoadFile)) == true)
                    {
                        PP.ReLoad();
                        toolStripStatusLabel1.Text = "添加成功";
                    }
                    else
                    {
                        toolStripStatusLabel1.Text = "添加失败";
                    }
                }

            }
            else
            {
                toolStripStatusLabel1.Text = "添加失败";
            }
        }

        private void button_exit_Click(object sender, EventArgs e)
        {
            Close();
        }

        private bool AddCam(string camname,string camip, string roadid, string roadname, string camtype,  string camposition,  string bz,string deviceid,string username,string password)
        {
            //<?xml version="1.0" encoding="UTF-8"?>
            //<param>
            //<road roadid="01" gbdm="123456789" roadname="测试路口一">
            //    <camera ip="172.16.22.252" type="HK9121" direction="由东向西" workmode="" bz=""></camera>
            //    <camera ip="172.16.22.250" type="HK9151" direction="由南向北" workmode="" bz=""></camera>
            //</road>
            //</param>

            string xpath = "descendant::road[@roadid='" + roadid + "']";
            string[] key = new string[8];
            string[] value = new string[8];
            key[0] = "cameraname";
            key[1] = "ip";
            key[2] = "type";
            key[3] = "position";
            key[4] = "bz";
            key[5] = "deviceid";
            key[6] = "username";
            key[7] = "password";
            value[0] = camname;
            value[1] = camip;
            value[2] = camtype;
            value[3] = camposition;
            value[4] = bz;
            value[5] = deviceid;
            value[6] = username;
            value[7] = password;
            bool ans = PP.AddConfig(PP.gRoadFile, xpath, "camera", "", key, value);
            if (ans == true)
            {
                if (PP.gRoadList != null)
                {
                    foreach (PP.RoadParam road in PP.gRoadList)
                    {
                        if (road.LKID == roadid)
                        {
                            if (road.JKCamList == null)
                            {
                                road.JKCamList = new List<JKCamera>();
                            }
                            if (road.JKCamList != null)
                            {
                                PP.JKCAMERATYPE t = (PP.JKCAMERATYPE)Enum.Parse(typeof(PP.JKCAMERATYPE), camtype);
                                if (t == PP.JKCAMERATYPE.DAHUA)
                                {
                                    DHJKCamera dhcam = new DHJKCamera();
                                    dhcam.m_RoadID = roadid;
                                    dhcam.m_RoadName = roadname;
                                    dhcam.m_CameraName = camname;
                                    dhcam.m_Position = (PP.CAMERAPOSITION)Enum.Parse(typeof(PP.CAMERAPOSITION), camposition);
                                    dhcam.m_IP = camip;
                                    dhcam.m_bz = bz;
                                    dhcam.m_deviceID = deviceid;
                                    dhcam.m_UserName = username;
                                    dhcam.m_PassWord = password;
                                    dhcam.m_PreSetPonitHelpName = new List<string>(PP.cPreSetPointNum);
                                    for (int i = 0; i < PP.cPreSetPointNum; i++)
                                    {
                                        if (i == 0)
                                        {
                                            dhcam.m_PreSetPonitHelpName.Add("录像预置点");
                                        }
                                        else
                                        {
                                            dhcam.m_PreSetPonitHelpName.Add("");
                                        }
                                        string xxpath = "descendant::camera[@ip='" + camip + "']";
                                        string[] s = new string[1];
                                        string[] ss = new string[1];
                                        s[0] = "index";
                                        ss[0] = i.ToString();
                                        PP.AddConfig(PP.gRoadFile, xxpath, "PreSetPoint", dhcam.m_PreSetPonitHelpName[i], s, ss);
                                    }
                                    road.JKCamList.Add(dhcam);
                                }
                                else if (t == PP.JKCAMERATYPE.HAIKANG)
                                {
                                    HKJKCamera hkcam = new HKJKCamera();
                                    hkcam.m_RoadID = roadid;
                                    hkcam.m_RoadName = roadname;
                                    hkcam.m_CameraName = camname;
                                    hkcam.m_Position = (PP.CAMERAPOSITION)Enum.Parse(typeof(PP.CAMERAPOSITION), camposition);
                                    hkcam.m_IP = camip;
                                    hkcam.m_bz = bz;
                                    hkcam.m_deviceID = deviceid;
                                    hkcam.m_UserName = username;
                                    hkcam.m_PassWord = password;
                                    hkcam.m_PreSetPonitHelpName = new List<string>(PP.cPreSetPointNum);
                                    for (int i = 0; i < PP.cPreSetPointNum; i++)
                                    {
                                        if (i == 0)
                                        {
                                            hkcam.m_PreSetPonitHelpName.Add("录像预置点");
                                        }
                                        else
                                        {
                                            hkcam.m_PreSetPonitHelpName.Add("");
                                        }
                                        string xxpath = "descendant::camera[@ip='" + camip + "']";
                                        string[] s = new string[1];
                                        string[] ss = new string[1];
                                        s[0] = "index";
                                        ss[0] = i.ToString();
                                        PP.AddConfig(PP.gRoadFile, xxpath, "PreSetPoint", hkcam.m_PreSetPonitHelpName[i], s, ss);
                                    }
                                    road.JKCamList.Add(hkcam);
                                }
                                break;
                            }
                        }
                    }
                }
            }
            return ans;

        }

    }
}
